//
//  PayCusView.m
//  UEAssistant
//
//  Created by mac on 2017/7/31.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "PayCusView.h"
#import "CouponTableViewCell.h"
#import "DashLineView.h"

@interface PayCusView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{

    NSDictionary            *_balaceData;
    NSArray                 *_tiketData;
    NSArray                 *_paytitles;
    NSMutableArray          *_cell_selectedModel;
    BOOL                    _isChooseTic;
    NSArray                 *_headerTitles;
    NSIndexPath             *_selectIndexpath_tiket;
    BOOL                    _isPasswordView;
    NSInteger               _pay_youhuiquan;
}

@property (nonatomic, strong) UILabel           *titleLabel;
@property (nonatomic, strong) UIButton          *cancel;
@property (nonatomic, strong) UIView            *lineView;
@property (nonatomic, strong) UILabel           *showMoney;
@property (nonatomic, strong) UITableView       *tableView;
@property (nonatomic, strong) UIButton          *sureButton;
@property (nonatomic, strong) UIButton          *moreBt;
@property (nonatomic, strong) UIImageView       *cancelImage;
@property (nonatomic, strong) UIButton          *closeBt;
@property (nonatomic, strong) UILabel           *showTikectInfo;
@property (nonatomic, strong) NSString          *alreaySelectTikect;
@property (nonatomic, strong) UIImageView       *seletImageOne;
@property (nonatomic, strong) UIImageView       *seletImageTwo;
@property (nonatomic, strong) UIImageView       *seletImageThr;
@property (nonatomic, strong) UIImageView       *seletImageFor;
@property (nonatomic, strong) UITextField       *passwordField;
@property (nonatomic, strong) UIButton          *payBackBt;
@property (nonatomic, strong) NSString          *youhuiquan_id;

@end


@implementation PayCusView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self getBanlaceAndTikectData];
        [self setUI];
    }
    return self;
}

- (void)setUI{

    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10*Radio;
    self.layer.masksToBounds = YES;
    
    _isChooseTic = NO;
    _paytitles = @[@"马券支付",@"马豆支付",@"马刀支付",@"马蹄支付"];
    _headerTitles = @[@"可用优惠券",@"不可用优惠券"];
    _cell_selectedModel = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 50*Radio)];
    _titleLabel.center = CGPointMake(Screen_Width/2, 25*Radio);
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(17)];
    _titleLabel.textColor = RGB(51, 51, 51, 1);
    _titleLabel.text = @"选择支付方式";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];

    _closeBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBt.frame = CGRectMake(Screen_Width-50*Radio, 15*Radio, 20*Radio, 20*Radio);
    [_closeBt setImage:[UIImage imageNamed:@"upupp"] forState:UIControlStateNormal];
    [_closeBt addTarget:self action:@selector(closePayTicketView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBt];
    [_closeBt setHidden:YES];
    
    _cancelImage = [[UIImageView alloc]initWithFrame:CGRectMake(15*Radio, 13*Radio, 24*Radio, 24*Radio)];
    _cancelImage.image = [UIImage imageNamed:@"关闭xxhdpi"];
    [self addSubview:_cancelImage];
    _cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancel.frame = CGRectMake(5*Radio, 0, 50*Radio, 50*Radio);
    [_cancel addTarget:self action:@selector(cancelClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancel];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 51*Radio, Screen_Width, 1)];
    _lineView.backgroundColor = RGB(227, 227, 227, 1);
    [self addSubview:_lineView];
    
    _showMoney = [[UILabel alloc]initWithFrame:CGRectMake(0, 60*Radio, Screen_Width, 30*Radio)];
    _showMoney.backgroundColor = [UIColor clearColor];
    _showMoney.font = [UIFont systemFontOfSize:FLEXIBLE(16)];
    _showMoney.textColor = RGB(51, 51, 51, 1);
    _showMoney.textAlignment = NSTextAlignmentCenter;
    _showMoney.attributedText = [self attributeStringWith:detailsModel.needPay];
    [self addSubview:_showMoney];

    _sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _sureButton.frame = CGRectMake(10*Radio, 370*Radio - 60*Radio, Screen_Width-20*Radio, 45*Radio);
    _sureButton.layer.cornerRadius = 5;
    _sureButton.layer.masksToBounds = YES;
    _sureButton.backgroundColor = RGB(57, 186, 241, 1);
    [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureButton setTitle:@"确认" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(SurePayButtonCliced) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(17)];
    [self addSubview:_sureButton];
    
    _passwordField = [[UITextField alloc]initWithFrame:CGRectMake(10, 80*Radio, Screen_Width-20, 0)];
    _passwordField.textAlignment = NSTextAlignmentLeft;
    _passwordField.backgroundColor = [UIColor whiteColor];
    _passwordField.borderStyle = UITextBorderStyleRoundedRect;
    _passwordField.keyboardType = UIKeyboardTypeASCIICapable;
    _passwordField.textColor = [UIColor blackColor];
    _passwordField.secureTextEntry = YES;
    _passwordField.font = [UIFont systemFontOfSize:FLEXIBLE(28)];
    _passwordField.returnKeyType = UIReturnKeyDone;
    _passwordField.delegate = self;
    _passwordField.hidden = YES;
    [self addSubview:_passwordField];
    
    [self addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CouponTableViewCell" bundle:nil] forCellReuseIdentifier:CouponCellID];

    
}

#pragma mark -- 优惠券和余额数据获取

- (void)getBanlaceAndTikectData{
    
    NSDictionary *param_ban = @{@"orderIds":detailsModel.orderId,@"teamId":detailsModel.ownerId};
    NSDictionary *param_tik = @{@"paid_user_type":detailsModel.pay_user_type,@"ownerId":detailsModel.ownerId};
    
    [AvailableNetWorking postWithUrl:owerTikketsCheck_connector params:param_tik successHandler:^(id responseObject) {
        
        NSLog(@"owerTikketRespond == %@",responseObject);
        if ([responseObject[@"code"] integerValue] == 200) {
            
            _tiketData = [NSArray arrayWithArray:responseObject[@"result"]];
            [AvailableNetWorking postWithUrl:owerBanlaceCheck_connector params:param_ban successHandler:^(id responseObject) {
                
                NSLog(@"responds === %@",responseObject);
                if ([responseObject[@"code"] integerValue] == 200) {
                    _balaceData = [NSDictionary dictionaryWithDictionary:responseObject];
                    [self.tableView reloadData];
                }
            } falureHandler:^(NSError *error) {
                
            }];
        }
        
    } falureHandler:^(NSError *error) {
        
    }];

    

    

}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (_isChooseTic) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (_isChooseTic) {
        if (section == 0) {
            return _tiketData.count;
        }else{
            return 0;
        }
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (_isChooseTic) {
        return 70*Radio;
    }
    return 50*Radio;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (_isChooseTic) {
        return 20*Radio;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (_isChooseTic) {
        
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 20*Radio)];
        back.backgroundColor = [UIColor clearColor];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 250, 20*Radio)];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        label.textColor = [UIColor darkGrayColor];
        label.text = [NSString stringWithFormat:@"%@(0张可用)",_headerTitles[section]];
        if (section == 0) {
            label.text = [NSString stringWithFormat:@"%@(%lu张可用)",_headerTitles[section],(unsigned long)_tiketData.count];
        }
        label.textAlignment = NSTextAlignmentLeft;
        [back addSubview:label];
        return back;
    }
    UIView *view = nil;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (_isChooseTic) {
        
        CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CouponCellID];
        cell.baseView.backgroundColor = RGB(244, 250, 253, 1);
        cell.baseView.layer.borderWidth = 1.0f;
        cell.baseView.layer.borderColor = RGB(154, 220, 247, 1).CGColor;
        cell.baseView.layer.cornerRadius = 10*Radio;
        cell.baseView.layer.masksToBounds = YES;
        cell.couponMoney.text = [NSString stringWithFormat:@"%@",_tiketData[indexPath.row][@"MONEY"]];
        cell.couponRemark.text = _tiketData[indexPath.row][@"NAME"];
        cell.couponDeadTime.text = [NSString stringWithFormat:@"%@-%@",[[[NSString stringWithFormat:@"%@",_tiketData[indexPath.row][@"START_DATE"]] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] substringToIndex:10],[[[NSString stringWithFormat:@"%@",_tiketData[indexPath.row][@"SEND_DATE"]] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] substringToIndex:10]];
        DashLineView *verticalDashLine = [[DashLineView alloc] initWithFrame:CGRectMake(100, 10, 0.5, 50) withLineLength:2 withLineSpacing:2 withLineColor:RGB(154, 220, 247, 1)];
        [cell addSubview:verticalDashLine];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath == _selectIndexpath_tiket) {
            cell.selectImage.image = [UIImage imageNamed:@"选中"];
        }else{
            cell.selectImage.image = [UIImage imageNamed:@""];
        }
        return cell;
    }
    else{
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = RGB(51, 51, 51, 1);
        cell.textLabel.font = [UIFont systemFontOfSize:FLEXIBLE(16)];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:FLEXIBLE(14)];
        cell.detailTextLabel.textColor = RGB(51,51, 51, 1);
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.text = _paytitles[indexPath.row];
        if (indexPath.row == 0) {
            _seletImageOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*Radio, 20*Radio)];
            _seletImageOne.backgroundColor = [UIColor clearColor];
            _seletImageOne.center = CGPointMake(22*Radio, 50*Radio/2);
            _seletImageOne.image = [UIImage imageNamed:@"选择"];
            [cell addSubview:_seletImageOne];
            
            if (_tiketData.count == 0) {
                [cell setUserInteractionEnabled:NO];
                cell.textLabel.textColor = [UIColor lightGrayColor];
            }
            
        }else if (indexPath.row == 1){
            _seletImageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*Radio, 20*Radio)];
            _seletImageTwo.backgroundColor = [UIColor clearColor];
            _seletImageTwo.center = CGPointMake(22*Radio, 50*Radio/2);
            _seletImageTwo.image = [UIImage imageNamed:@"选择"];
            [cell addSubview:_seletImageTwo];

            cell.detailTextLabel.text = [self judeNullWithString:[NSString stringWithFormat:@"(剩余%@马豆)",_balaceData[@"ue_mode"]]];
            if ([_balaceData[@"ue_mode"] doubleValue] == 0) {
                cell.detailTextLabel.text = @"(剩余不足)";
                [cell setUserInteractionEnabled:NO];
                cell.textLabel.textColor = [UIColor lightGrayColor];
                cell.detailTextLabel.textColor = [UIColor lightGrayColor];
            }

        }else if (indexPath.row == 2){
            _seletImageThr = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*Radio, 20*Radio)];
            _seletImageThr.backgroundColor = [UIColor clearColor];
            _seletImageThr.center = CGPointMake(22*Radio, 50*Radio/2);
            _seletImageThr.image = [UIImage imageNamed:@"选中"];
            [cell addSubview:_seletImageThr];

            cell.detailTextLabel.text = [self judeNullWithString:[NSString stringWithFormat:@"(剩余%@马刀)",_balaceData[@"money"]]];
            if ([_balaceData[@"money"] doubleValue] == 0) {
                cell.detailTextLabel.text = @"(剩余不足)";
                [cell setUserInteractionEnabled:NO];
                cell.textLabel.textColor = [UIColor lightGrayColor];
                cell.detailTextLabel.textColor = [UIColor lightGrayColor];            }

        }else if (indexPath.row == 3){
            _seletImageFor = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*Radio, 20*Radio)];
            _seletImageFor.backgroundColor = [UIColor clearColor];
            _seletImageFor.center = CGPointMake(22*Radio, 50*Radio/2);
            _seletImageFor.image = [UIImage imageNamed:@"选择"];
            [cell addSubview:_seletImageFor];

            cell.detailTextLabel.text = [self judeNullWithString:[NSString stringWithFormat:@"(剩余%@马蹄)",_balaceData[@"ue_coupons"]]];
            if ([_balaceData[@"ue_coupons"] doubleValue] == 0) {
                cell.detailTextLabel.text = @"(剩余不足)";
                [cell setUserInteractionEnabled:NO];
                cell.textLabel.textColor = [UIColor lightGrayColor];
                cell.detailTextLabel.textColor = [UIColor lightGrayColor];
            }
        }
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 49*Radio, Screen_Width, 1)];
        line.backgroundColor = RGB(227, 227, 227, 1);
        if (indexPath.row != 3) {
            [cell addSubview:line];
        }
        if (indexPath.row == 0) {
            _moreBt = [UIButton buttonWithType:UIButtonTypeCustom];
            _moreBt.frame = CGRectMake(Screen_Width-50*Radio, 15*Radio, 20*Radio, 20*Radio);
            [_moreBt setImage:[UIImage imageNamed:@"unfold"] forState:UIControlStateNormal];
            [_moreBt addTarget:self action:@selector(choosePaytickets) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:_moreBt];
            
            _showTikectInfo = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width-200*Radio, 15*Radio, 140*Radio, 20*Radio)];
            _showTikectInfo.backgroundColor = [UIColor clearColor];
            _showTikectInfo.font = [UIFont systemFontOfSize:FLEXIBLE(14)];
            _showTikectInfo.textColor = RGB(51, 51, 51, 1);
            _showTikectInfo.text = @"请选择优惠券";
            if (_alreaySelectTikect) {
                _showTikectInfo.text = _alreaySelectTikect;
            }
            _showTikectInfo.textAlignment = NSTextAlignmentRight;
            _showTikectInfo.hidden = YES;
            [cell addSubview:_showTikectInfo];

        }
        if ([_cell_selectedModel[indexPath.row] boolValue] == NO) {
            switch (indexPath.row) {
                case 0:
                    _seletImageOne.image = [UIImage imageNamed:@"选择"];
                    break;
                case 1:
                    _seletImageTwo.image = [UIImage imageNamed:@"选择"];
                    break;
                case 2:
                    _seletImageThr.image = [UIImage imageNamed:@"选中"];
                    break;
                case 3:
                    _seletImageFor.image = [UIImage imageNamed:@"选择"];
                    break;
                default:
                    break;
            }

            if (indexPath.row == 0) {
                _showTikectInfo.hidden = YES;
            }
        }
        else{
            switch (indexPath.row) {
                case 0:
                    _seletImageOne.image = [UIImage imageNamed:@"选中"];
                    break;
                case 1:
                    _seletImageTwo.image = [UIImage imageNamed:@"选中"];
                    break;
                case 2:
                    _seletImageThr.image = [UIImage imageNamed:@"选中"];
                    break;
                case 3:
                    _seletImageFor.image = [UIImage imageNamed:@"选中"];
                    break;
                default:
                    break;
            }

            if (indexPath.row == 0) {
                _showTikectInfo.hidden = NO;
            }
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isChooseTic) {
        
        CouponTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.selectImage.image = [UIImage imageNamed:@"选中"];
        _selectIndexpath_tiket = indexPath;
        //返回支付界面
        [self closePayTicketView];
        //选中马券支付
        [_cell_selectedModel replaceObjectAtIndex:0 withObject:@"1"];
        _seletImageOne.image = [UIImage imageNamed:@"选中"];
        //存储优惠券的面值
        _alreaySelectTikect = [NSString stringWithFormat:@"已选择%@元优惠券",cell.couponMoney.text];
        _pay_youhuiquan = [cell.couponMoney.text integerValue];
        _youhuiquan_id = [NSString stringWithFormat:@"%@",_tiketData[indexPath.row][@"COUPON_SEND_ID"]];
    }
    else{
        
        if ([_cell_selectedModel[indexPath.row] boolValue] == NO) {
            switch (indexPath.row) {
                case 0:
                    _seletImageOne.image = [UIImage imageNamed:@"选中"];
                    break;
                case 1:
                    _seletImageTwo.image = [UIImage imageNamed:@"选中"];
                    break;
                case 2:
                    _seletImageThr.image = [UIImage imageNamed:@"选中"];
                    break;
                case 3:
                    _seletImageFor.image = [UIImage imageNamed:@"选中"];
                    break;
                default:
                    break;
            }
            [_cell_selectedModel replaceObjectAtIndex:indexPath.row withObject:@"1"];
            if (indexPath.row == 0) {
                _showTikectInfo.hidden = NO;
            }
        }
        else{
            switch (indexPath.row) {
                case 0:
                    _seletImageOne.image = [UIImage imageNamed:@"选择"];
                    break;
                case 1:
                    _seletImageTwo.image = [UIImage imageNamed:@"选择"];
                    break;
                case 2:
                    _seletImageThr.image = [UIImage imageNamed:@"选中"];
                    break;
                case 3:
                    _seletImageFor.image = [UIImage imageNamed:@"选择"];
                    break;
                default:
                    break;
            }
            [_cell_selectedModel replaceObjectAtIndex:indexPath.row withObject:@"0"];
            if (indexPath.row == 0) {
                _showTikectInfo.hidden = YES;
            }

        }
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (_isChooseTic) {
        CouponTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.selectImage.image = [UIImage imageNamed:@""];
    }
}

- (NSMutableAttributedString *)attributeStringWith:(NSString *)money {
    NSString *str = [NSString stringWithFormat:@"应付%@马刀", money];
    NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:money];
    [ats setAttributes:@{NSForegroundColorAttributeName:RGB(231, 59, 62, 1),NSFontAttributeName:[UIFont systemFontOfSize:15]} range:range];
    return ats;
}

#pragma mark judeNUll---

- (NSString *)judeNullWithString:(id)string{
    
//    NSLog(@"dasdsadsadsadsad == %@",[string substringWithRange:NSMakeRange(4, 4)]);
//    if ([[string substringWithRange:NSMakeRange(4, 4)] isEqualToString:@"null"]) {
//        return @"(正在计算...)";
//    }
//    else{
    
        return string;
//    }
}

#pragma mark -- 视图内子视图更新部分
//选择优惠券
- (void)choosePaytickets{

    _isChooseTic = YES;
    [_sureButton setHidden:YES];
    [UIView animateKeyframesWithDuration:0.3
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionLayoutSubviews
                              animations:^{
                                  
                                  [_showMoney setHidden:YES];
                                  [_closeBt setHidden:NO];
                                  [_cancel setHidden:YES];
                                  [_cancelImage setHidden:YES];
                                  _titleLabel.center = CGPointMake(115, 25*Radio);
                                  _titleLabel.textAlignment = NSTextAlignmentLeft;
                                  _titleLabel.text = @"马券支付";
                                  
                                  self.tableView.frame = CGRectMake(0, 95*Radio-43*Radio, Screen_Width, 200*Radio+43*Radio+70*Radio);
                                  self.tableView.scrollEnabled = YES;
                                  [self.tableView reloadData];

                              }
                              completion:^(BOOL finished) {
                                  
                              }];
}

//收起优惠券
- (void)closePayTicketView{

    _isChooseTic = NO;
    [_sureButton setHidden:NO];
    [UIView animateKeyframesWithDuration:0.3
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionLayoutSubviews
                              animations:^{
                                  
                                  [_showMoney setHidden:NO];
                                  [_closeBt setHidden:YES];
                                  [_cancel setHidden:NO];
                                  [_cancelImage setHidden:NO];
                                  _titleLabel.center = CGPointMake(Screen_Width/2, 25*Radio);
                                  _titleLabel.textAlignment = NSTextAlignmentCenter;
                                  _titleLabel.text = @"选择支付方式";
                                  
                                  self.tableView.frame = CGRectMake(0, 95*Radio, Screen_Width, 200*Radio);
                                  self.tableView.scrollEnabled = NO;
                                  _seletImageOne.image = [UIImage imageNamed:@"选中"];
                                  [self.tableView reloadData];

                              }
                              completion:^(BOOL finished) {
                                  
                              }];

}

- (void)cancelClicked{

    if (!_isPasswordView) {//收起支付视图

        detailsModel.orderId = @"";
        [[NSNotificationCenter defaultCenter]postNotificationName:@"hiddenCorView" object:nil];
    }
    else{//收起密码输出框

        _isPasswordView = NO;
        [_passwordField resignFirstResponder];
        [UIView animateKeyframesWithDuration:0.3
                                       delay:0.0
                                     options:UIViewKeyframeAnimationOptionLayoutSubviews
                                  animations:^{
                                      
                                      [_sureButton setHidden:NO];
                                      [_showMoney setHidden:NO];
                                      [_closeBt setHidden:YES];
//                                      _cancelImage.image = [UIImage imageNamed:@"关闭xxhdpi"];
                                      _titleLabel.text = @"选择支付方式";
                                      self.tableView.frame = CGRectMake(0, 95*Radio, Screen_Width, 200*Radio);
                                      _passwordField.frame = CGRectMake(10, 80*Radio, Screen_Width-20, 0);
                                      _passwordField.hidden = YES;
                                  }
                                  completion:^(BOOL finished) {
                                      
                                  }];

    }

}

//确认支付 弹出密码输入框
- (void)SurePayButtonCliced{

    _isPasswordView = YES;
    [UIView animateKeyframesWithDuration:0.3
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionLayoutSubviews
                              animations:^{
                                  
                                  [_sureButton setHidden:YES];
                                  [_showMoney setHidden:YES];
                                  [_closeBt setHidden:YES];
//                                  _cancelImage.image = [UIImage imageNamed:@"返回"];
                                  _titleLabel.text = @"输入密码";
                                  self.tableView.frame = CGRectMake(0, 95*Radio-43*Radio, Screen_Width, 0);
                                  _passwordField.frame = CGRectMake(10, 80*Radio, Screen_Width-20, 40);
                                  _passwordField.hidden = NO;
                              }
                              completion:^(BOOL finished) {
                                  
                                  [_passwordField becomeFirstResponder];
                              }];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    //计算付款金额
    double youhuiquan;
    double madou;
    double madao;
    double mati;
    
    //优惠券
    if (_tiketData.count > 0 && [_cell_selectedModel[0] boolValue] == YES) {
        
        youhuiquan = _pay_youhuiquan;
    }else{
        
        youhuiquan = 0;
        _youhuiquan_id = @"";
    }
    //马豆
    if ([_balaceData[@"ue_mode"] doubleValue] > 0 && [_cell_selectedModel[1] boolValue] == YES) {
        
        if ([_balaceData[@"ue_mode"] doubleValue] >= [detailsModel.needPay doubleValue] - youhuiquan) {
            madou = [detailsModel.needPay doubleValue] - youhuiquan;
        }
        else{
            madou = [_balaceData[@"ue_mode"] doubleValue];
        }
    }else{
        
        madou = 0;
    }
    //马蹄
    if ([_balaceData[@"ue_coupons"] doubleValue] > 0 && [_cell_selectedModel[3] boolValue] == YES) {
        
        if ([_balaceData[@"ue_coupons"] doubleValue] > ([detailsModel.needPay doubleValue] - youhuiquan - madou)/2) {
            mati = ([detailsModel.needPay doubleValue] - youhuiquan - madou)/2;
        }else{
            mati = [detailsModel.needPay doubleValue];
        }
        
    }else{
        
        mati = 0;
    }
    //马刀(默认支付)
    madao = [detailsModel.needPay doubleValue] - youhuiquan - madou - mati;
    
    
    NSDictionary *passParams = @{@"ue_coupons":[NSString stringWithFormat:@"%.2f",mati],
                                 @"ue_mode":[NSString stringWithFormat:@"%.2f",madou],
                                 @"money":[NSString stringWithFormat:@"%.2f",madao],
                                 @"paid_user_type":detailsModel.pay_user_type,
                                 @"order_id":detailsModel.orderId,
                                 @"teamId":detailsModel.ownerId,
                                 @"ownerId":detailsModel.ownerId,
                                 @"payPass":textField.text,
                                 @"deduction_money":[NSString stringWithFormat:@"%.2f",youhuiquan],
                                 @"deduction_id": _youhuiquan_id};
    
    NSDictionary *payParams = @{@"order_id":detailsModel.orderId,
                                @"paid_user_type":detailsModel.pay_user_type,
                                @"teamId":detailsModel.ownerId,
                                @"ue_coupons":[NSString stringWithFormat:@"%.2f",mati],
                                @"ue_mode":[NSString stringWithFormat:@"%.2f",madou],
                                @"money":[NSString stringWithFormat:@"%.2f",madao],
                                @"deduction_money":[NSString stringWithFormat:@"%.2f",youhuiquan],
                                @"deduction_id":_youhuiquan_id};
    NSLog(@"payParams == %@",payParams);
    [AvailableNetWorking postWithUrl:owerPasswordCheck_connector params:passParams successHandler:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {//密码正确直接支付
            
            NSLog(@"密码验证正确");
            [AvailableNetWorking postWithUrl:owerPayMoney_connector params:payParams successHandler:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    
                    detailsModel.orderId = @"";
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshOrderListData" object:nil];
                    
                }
                [textField resignFirstResponder];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"hiddenCorView" object:nil];
                [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:NO];

            } falureHandler:^(NSError *error) {
                
            }];
            
        }else{//出现错误
        
            [textField resignFirstResponder];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"hiddenCorView" object:nil];
            [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:NO];
        }
        
    } falureHandler:^(NSError *error) {
        
    }];
    return YES;
}

#pragma mark -- getter
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 95*Radio, Screen_Width, 200*Radio) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}


@end
